/**
 * 
 */
package com.wasion.pwportable.protocol.p2;

/**
 * @className ProtocolUtil.java
 * @author W11045
 * @description
 * @date 2013-11-25
 * 
 */
public class ProtocolUtil {

	// 流程名称的枚举类型
	public enum FlowCMDEnum {
		RESET_CMD("复位终端", 0), // 0
		INITIALIZE_CMD("初始化", 1), // 1
		ALL_DATA_COLLECT_CMD("总召唤", 2), GROUP_DATA_COLLECT_CMD("分组召唤", 3), REMOTE_CON_PREPARE_CMD(
				"遥控-预置", 4), REMOTE_CON_CANCEL_CMD("遥控-撤销", 5), REMOTE_CON_EXEC_CMD(
				"遥控-执行", 6), SYNC_TIME_CMD("对时", 7), //
		TEST_CMD("测试命令", 8), AUTO_UPLOAD_CMD("", 9), AUTO_UPLOAD_CANCEL_CMD("",
				10), GET_PARAM_CMD("扩展参数-读取", 11), SET_PARAM_CMD("扩展参数-写入", 12), COMM_PARAM_SEND_CMD(
				"", 13), COMM_PARAM_CANCEL_CMD("", 14), COMM_PARAM_EXEC_BEGIN_CMD(
				"", 15), FTP_PARAM_SEND_BEGIN_CMD("", 16), FTP_PARAM_SEND_CANCEL_CMD(
				"", 17), FTP_PARAM_SEND_EXEC_CMD("", 18), SET_PASSWORD_CMD(
				"扩展参数-设置密码", 19), HIS_COLLCET_CMD("冻结数据-读取", 20), HIS_ALARM_CMD(
				"", 21), GET_REMOTE_PARAM_CMD("远程参数读取", 22), SET_REMOTE_PARAM_CMD(
				"远程参数设置", 23), ExecutCmd_BUTT("End", 24);

		// 成员变量
		private String name;
		private int index;

		// 构造方法
		private FlowCMDEnum(String name, int index) {
			this.name = name;
			this.index = index;
		}

		// 普通方法
		public static String getName(int index) {
			for (FlowCMDEnum c : FlowCMDEnum.values()) {
				if (c.getIndex() == index) {
					return c.name;
				}
			}
			return null;
		}

		// get set 方法
		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getIndex() {
			return index;
		}

		public void setIndex(int index) {
			this.index = index;
		}
	}

	// 流程名称的枚举类型
	public enum ExecutCmdRspEnum {
		EXECUT_RSP_SUCC(0), EXECUT_RSP_FAIL(1), EXECUT_RSP_REFUSE(2), EXECUT_RSP_DATA(
				3), EXECUT_RSP_UNEXPECT(4), EXECUT_RSP_UNEXPECT_DATA(5), ExecutCmdRsp_BUTT(
				6);

		// 成员变量
		private int index;

		// 构造方法
		private ExecutCmdRspEnum(int index) {
			this.index = index;
		}

		// 普通方法
		public static String getName(int index) {
			for (ExecutCmdRspEnum c : ExecutCmdRspEnum.values()) {
				if (c.getIndex() == index) {
					return c.name();
				}
			}
			return null;
		}

		public int getIndex() {
			return index;
		}

		public void setIndex(int index) {
			this.index = index;
		}
	}

	public static String getParamsValue(String params) {
		String paramStr = "";
		if (params == null) {
			paramStr = "00000000";
		} else {
			paramStr = params;
			for (int i = params.length(); i < 8; i++) {
				paramStr = "0" + paramStr;
			}
		}

		return paramStr.substring(6) + paramStr.substring(4, 6)
				+ paramStr.substring(2, 4) + paramStr.substring(0, 2);
	}

	/**
	 * 
	 * @Title: parseParam2FlowRuleNeed
	 * @Description: 将参数转换成流程规则的需要形式
	 * @param
	 * @return
	 * @throws
	 */
	public static String parseParam2FlowRuleNeed(int param) {

		String strHex = Integer.toHexString(param & 0xFFFFFFFF);
		int len = strHex.length();
		for (int i = len; i < 8; i++) {
			strHex = "0" + strHex;
		}

		return strHex.substring(6) + strHex.substring(4, 6)
				+ strHex.substring(2, 4) + strHex.substring(0, 2);
	}
}
